package com.qujia.station.controller;

import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.ErrorCode;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.Station;
import com.qujia.system.domain.XcxActualPaymentDto;
import com.qujia.station.domain.form.StaffForm;
import com.qujia.station.domain.vo.StaffVO;
import com.qujia.station.service.IStaffService;
import com.qujia.station.service.IStationService;
import com.qujia.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "员工管理")
@RestController
@RequestMapping("staff")
public class StaffController extends BaseController {


    public static final int PASSWORD_MAX_LENGTH = 16;
    public static final int PASSWORD_MIN_LENGTH = 6;

    @Autowired
    private IStaffService staffService;

    @Resource
    private ISysUserService sysUserService;

    @Autowired
    private IStationService stationService;

    @ApiOperation("查询员工列表")
    @GetMapping("query")
    public TableDataInfo queryList(StaffForm form){
        StaffVO staffVO = StaffVO.of(form);
        staffVO.setUserLevel(UserConstants.LEVEL_THREE);
        List<StaffVO> staffVOS = staffService.queryStaffList(staffVO);
        return getDataTable(staffVOS);
    }

    @ApiOperation("添加员工")
    @PostMapping("insert")
    public AjaxResult insert(@RequestBody StaffVO form){
        String password = form.getPassword();
        int length = StringUtils.length(password);
        if (StringUtils.isBlank(password) || length < PASSWORD_MIN_LENGTH || length > PASSWORD_MAX_LENGTH) {
            throw new CustomException(ErrorCode.VALIDATE_ERROR);
        }
        staffService.insertStaff(form);
        return AjaxResult.success();
    }

    @ApiOperation("更新员工")
    @PostMapping("update")
    public AjaxResult update(@RequestBody StaffVO form){
        String password = form.getPassword();
        int length = StringUtils.length(password);
        if (StringUtils.isNotBlank(password) && length < PASSWORD_MIN_LENGTH || length > PASSWORD_MAX_LENGTH) {
            throw new CustomException(ErrorCode.VALIDATE_ERROR);
        }
        staffService.updateStaff(form);
        return AjaxResult.success();
    }

    @ApiOperation("根据id查询员工")
    @GetMapping("getById")
    public AjaxResult getById(@RequestParam("id") Long id){
        StaffVO staff = staffService.getById(id);
        return AjaxResult.success(staff);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("delete")
    public AjaxResult delete(@RequestParam("ids") Long[] ids){
        staffService.deleteStaffByIds(ids);
        return AjaxResult.success();
    }

    @ApiOperation("查询油站下枪号列表")
    @GetMapping("queryGunList")
    public AjaxResult queryStationGunList(@RequestParam @Validated @NotNull Long stationId){
        return AjaxResult.success(staffService.queryStationGunList(stationId));
    }


    @GetMapping("/getUserPermissions")
    @ApiModelProperty(value = "报表小程序返回权限")
    public AjaxResult getUserPermissions(@ApiParam(value = "用户id")   @RequestParam(value = "userId") Long userId){
        return  AjaxResult.success(staffService.getUserPermissions(userId));
    }

    /**判断用户是否是员工*/
    @GetMapping("/employeeSign")
    @ApiModelProperty(value = "判断用户是否是员工")
    public AjaxResult getEmployeeSign(){
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if(Objects.nonNull(user.getUserLevel()) && UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            String sign = sysUserService.getEmployeeSign(user.getUserId());
            // 员工权限
            if ("1".equals(sign)) {
                return AjaxResult.success(sign);
            }
        }

        return AjaxResult.success("0");
    }

    /**
     * 员工账号首页数据按当日全部或根据班结时间进行展示
     * */
    @GetMapping("/employee/show/list")
    @ApiOperation(value = "员工账号首页数据按当日全部或根据班结时间进行展示")
    public AjaxResult employeeShowList(){
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        // 判断用户等级 空的用户等级是admin
        if(Objects.isNull(user.getUserLevel()) || UserConstants.LEVEL_ONE.equals(user.getUserLevel())){
            List<String> stringList = sysUserService.selectByEmployee(null);
            return AjaxResult.success(stringList);
        } else if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            // 油企账号 针对一个油企可以有多个油企账号
            if(Objects.isNull(user.getEnterpriseId())){
                return AjaxResult.error("油企账号没有油企id");
            }
            List<Station> stationList = stationService.getStationByEnterpriseId(user.getEnterpriseId());
            List<Long> stationIdList = new ArrayList<>();
            for (Station station : stationList) {
                if(Objects.nonNull(station) && Objects.nonNull(station.getStationId())){
                    stationIdList.add(station.getStationId());
                }
            }
            List<String> stringList = sysUserService.selectByEmployee(stationIdList);
            return AjaxResult.success(stringList);
        } else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            if(Objects.isNull(user.getStationId())){
                return AjaxResult.error("油站账号没有油站id");
            }
            List<Long> stationIdList = new ArrayList<>();
            stationIdList.add(user.getStationId());
            List<String> stringList = sysUserService.selectByEmployee(stationIdList);
            return AjaxResult.success(stringList);
        }

        return AjaxResult.error("权限不足");
    }
    /**
     * 小程序实付
     * */
    @GetMapping("/employee/show/xcxActualPayment")
    @ApiOperation(value = "小程序是否展示实付0默认不显示1显示")
    public AjaxResult xcxActualPayment(XcxActualPaymentDto xcxActualPayment){

        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            // 油企账号 针对一个油企可以有多个油企账号
            if(Objects.isNull(user.getEnterpriseId())){
                return AjaxResult.error("油企账号没有油企id");
            }
            xcxActualPayment.setEnterpriseId(user.getEnterpriseId());
            sysUserService.xcxActualPayment(xcxActualPayment);
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("权限不足");
    }


    /**
     * 小程序实付
     * */
    @GetMapping("/employee/show/xcxActualPaymentDetail")
    @ApiOperation(value = "查看小程序是否展示实付")
    public AjaxResult xcxActualPaymentDetail(Long enterpriseId){

        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            // 油企账号 针对一个油企可以有多个油企账号
            if(Objects.isNull(user.getEnterpriseId())){
                return AjaxResult.error("油企账号没有油企id");
            }
            XcxActualPaymentDto xcxActualPayment = new XcxActualPaymentDto();
            xcxActualPayment.setEnterpriseId(enterpriseId);
            XcxActualPaymentDto XcxActualPaymentDtoResult =  sysUserService.xcxActualPaymentDetail(xcxActualPayment);
            return AjaxResult.success(XcxActualPaymentDtoResult);
        }
        return AjaxResult.error("权限不足");
    }

    /**
     * 修改员工账号首页数据按当日全部或根据班结时间进行展示权限
     * */
    @GetMapping("/employee/show/update")
    @ApiOperation(value = "修改员工账号首页数据按当日全部或根据班结时间进行展示权限")
    public AjaxResult employeeShowUpdate(@RequestParam("employeeShow") String employeeShow){
        if(StringUtils.isEmpty(employeeShow)){
            return AjaxResult.error("请求参数异常");
        }
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        // 判断用户等级 空的用户等级是admin
        if(Objects.isNull(user.getUserLevel()) || UserConstants.LEVEL_ONE.equals(user.getUserLevel())){
            sysUserService.updateEmployeeShow(null,employeeShow);
            return AjaxResult.success("修改成功");
        } else if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            // 油企账号 针对一个油企可以有多个油企账号
            if(Objects.isNull(user.getEnterpriseId())){
                return AjaxResult.error("油企账号没有油企id");
            }
            List<Station> stationList = stationService.getStationByEnterpriseId(user.getEnterpriseId());
            List<Long> stationIdList = new ArrayList<>();
            for (Station station : stationList) {
                if(Objects.nonNull(station) && Objects.nonNull(station.getStationId())){
                    stationIdList.add(station.getStationId());
                }
            }
            sysUserService.updateEmployeeShow(stationIdList,employeeShow);
            return AjaxResult.success("修改成功");
        } else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            if(Objects.isNull(user.getStationId())){
                return AjaxResult.error("油站账号没有油站id");
            }
            List<Long> stationIdList = new ArrayList<>();
            stationIdList.add(user.getStationId());
            sysUserService.updateEmployeeShow(stationIdList,employeeShow);
            return AjaxResult.success("修改成功");
        }

        return AjaxResult.error("权限不足");
    }
}
